import requests
import json
import os
import pytz
import datetime

def get_time_str() -> str:
    return datetime.datetime.now(tz=pytz.timezone('UTC')).strftime("%Y-%m-%dT%H:%M:%S.%fZ")

def simple_text_log(log_type: str, msg: str, verbose=0):
    tmp_config = json.load(open(os.path.join('config', 'conf.json'), 'rt'))
    remote_ip = f"{tmp_config['es_ip']}/logs-{log_type}/_doc"
    payload = {
        "@timestamp": get_time_str(),
        "type": log_type,
        "content": {
            "msg": msg
        }
    }

    print(f"{log_type}: {msg}")

    print(remote_ip)

    try:
        r = requests.post(url=remote_ip, json=payload)
        if verbose:
            print(r.text)
    except Exception as err:
        print(err)


def simple_metric_log(log_type: str, name: str, value, verbose=0):
    tmp_config = json.load(open(os.path.join('config', 'conf.json'), 'rt'))
    remote_ip = f"{tmp_config['es_ip']}/logs-{log_type}/_doc"
    payload = {
        "@timestamp": get_time_str(),
        "type": log_type,
        "content": {
            "name": name,
            "value": value
        }
    }

    print(f"{log_type}: {name} = {value}")

    print(remote_ip)

    try:
        r = requests.post(url=remote_ip, json=payload)
        if verbose:
            print(r.text)
    except Exception as err:
        print(err)
